from flask import Blueprint, jsonify, request
from functools import wraps
from app.utils.logger import logger

base_bp = Blueprint('base', __name__, url_prefix='/api')

def api_response(success=True, data=None, message="", status_code=200):
    """统一API响应格式"""
    response = {
        "success": success,
        "data": data,
        "message": message
    }
    return jsonify(response), status_code

def error_handler(f):
    """接口错误处理装饰器"""
    @wraps(f)
    def decorated_function(*args, **kwargs):
        try:
            return f(*args, **kwargs)
        except Exception as e:
            logger.error(f"API Error: {str(e)}", exc_info=True)
            return api_response(
                success=False,
                message=f"服务器内部错误: {str(e)}",
                status_code=500
            )
    return decorated_function

@base_bp.route('/health', methods=['GET'])
def health_check():
    """服务健康检查接口"""
    return api_response(data={"status": "healthy"}, message="服务运行正常")
